package org.muxiao.seckill.config;

import org.muxiao.seckill.pojo.User;

/**
 * ThreadLocal 实现线程安全
 */
public class UserContext {

    /**
     * 每个线程都有自己的 ThreadLocal, 把共享数据存放到这里, 保证线程安全
     */
    private static ThreadLocal<User> userThreadLocal = new ThreadLocal<>();

    /**
     * 将 user 存放到 ThreadLocal 中
     * @param user
     */
    public static void setUser(User user) {
        userThreadLocal.set(user);
    }

    /**
     * 从 ThreadLocal 中获取 user
     * @return
     */
    public static User getUser() {
        return userThreadLocal.get();
    }
}
